(*
 * Materialien zu den zentralen Abiturpruefungen
 * im Fach Informatik ab 2012 in Nordrhein-Westfalen.
 *
 * Klasse TBinaryTree
 *
 * NW-Arbeitsgruppe:
 * Materialentwicklung zum Zentralabitur im Fach Informatik
 *
 * Version 2010-12-28
 *)

unit mBinaryTree;

interface

type
  TBinaryTree = class
    private
      content: TObject;
      leftTree, rightTree: TBinaryTree;
    public
      constructor create; overload; virtual;
      constructor create(pObject: TObject); overload; virtual;
      constructor create(pObject: TObject; pLeftTree, pRightTree: TBinaryTree);
        overload; virtual;
      function isEmpty: boolean; virtual;
      procedure setObject(pObject: TObject); virtual;
      function getObject: TObject; virtual;
      procedure setLeftTree(pTree: TBinaryTree); virtual;
      procedure setRightTree(pTree: TBinaryTree); virtual;
      function getLeftTree: TBinarytree; virtual;
      function getRightTree: TBinaryTree; virtual;
      procedure setEmpty; virtual;
      destructor destroy; override;
      procedure removeCompletely; virtual;
    private
      procedure removeTree(pTree: TBinaryTree); virtual;
  end;

implementation

  constructor TBinaryTree.create;
  begin
   content := nil;
   leftTree := nil;
   rightTree := nil;
  end;

  constructor TBinaryTree.create(pObject: TObject);
  begin
    if pObject = nil then
    begin
     content := nil;
     leftTree := nil;
     rightTree := nil;
    end
    else
    begin
      content := pObject;
      leftTree := TBinaryTree.create;
      rightTree := TBinaryTree.create;
    end;
  end;

  constructor TBinaryTree.create
    (pObject: TObject; pLeftTree, pRightTree: TBinaryTree);
  begin
    if pObject = nil then
    begin
      content := nil;
      leftTree := nil;
      rightTree := nil;
    end
    else
    begin
      content := pObject;
      if pLeftTree <> nil then
        leftTree := pLeftTree
      else
        leftTree := TBinaryTree.create;
      if pRightTree <> nil then
        rightTree := pRightTree
      else
        rightTree := TBinaryTree.create;
    end;
  end;

  function TBinaryTree.isEmpty: boolean;
  begin
    result := content = nil;
  end;

  procedure TBinaryTree.setObject(pObject: TObject);
  begin
    if pObject <> nil then
    begin
      if self.isEmpty then
       begin
         leftTree := TBinaryTree.create;
         rightTree := TBinaryTree.create;
       end;
       content := pObject;
    end;
  end;

  function TBinaryTree.getObject: TObject;
  begin
    result := content;
  end;

  procedure TBinaryTree.setLeftTree(pTree: TBinaryTree);
  begin
    if not self.isEmpty and (pTree <> nil) then
      leftTree := pTree;
  end;

  procedure TBinaryTree.setRightTree(pTree: TBinaryTree);
  begin
    if not self.isEmpty and (pTree <> nil) then
      rightTree := pTree;
  end;

  function TBinaryTree.getLeftTree: TBinaryTree;
  begin
    if not self.isEmpty then
      result := leftTree
    else
      result := nil;
  end;

  function TBinaryTree.getRightTree: TBinaryTree;
  begin
    if not self.isEmpty then
      result := rightTree
    else
      result := nil;
  end;

 procedure TBinaryTree.setEmpty;
  begin
   content := nil;
   if leftTree <> nil then
     leftTree.destroy;
   leftTree := nil;
   if rightTree <> nil then
     rightTree.destroy;
   rightTree := nil;
  end;

  procedure TBinaryTree.removeTree(pTree: TBinaryTree);
  begin
    if pTree <> nil then
    begin
      if not pTree.isEmpty then
      begin
        removeTree(pTree.getLeftTree);
        removeTree(pTree.getRightTree);
        if pTree.getLeftTree.isEmpty and pTree.getRightTree.isEmpty then
          pTree.setEmpty;
      end;
    end;
  end;

  procedure TBinaryTree.removeCompletely;
  begin
    self.removeTree(self);
  end;

  destructor TBinaryTree.destroy;
  begin
   inherited destroy;
  end;

end.


